| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356 |
12x
12x
12x
12x
12x
12x
107x
107x
107x
107x
12x
282x
7x
282x
90x
192x
62x
130x
12x
475x
126x
349x
6x
6x
6x
5x
1x
6x
6x
11x
11x
11x
8x
3x
11x
11x
11x
343x
343x
343x
343x
343x
343x
343x
233x
233x
233x
233x
742x
110x
110x
110x
110x
343x
343x
343x
724x
724x
343x
724x
724x
689x
35x
475x
12x
12x
902x
12x
8x
12x
1012x
12x
130x
118x
118x
12x
130x
130x
130x
130x
130x
130x
49x
49x
49x
1x
49x
49x
49x
24x
12x
24x
25x
13x
25x
25x
25x
5x
5x
5x
20x
81x
81x
81x
65x
33x
33x
65x
16x
12x
| /**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { RangedFilter } from './RangedFilter';
import { ChildrenNode } from '../../snap/ChildrenNode';
import { Node, NamedNode } from '../../snap/Node';
import { assert } from '@firebase/util';
import { Change } from '../Change';
import { NodeFilter } from './NodeFilter';
import { Index } from '../../snap/indexes/Index';
import { IndexedFilter } from './IndexedFilter';
import { QueryParams } from '../QueryParams';
import { Path } from '../../util/Path';
import { CompleteChildSource } from '../CompleteChildSource';
import { ChildChangeAccumulator } from '../ChildChangeAccumulator';
/**
* Applies a limit and a range to a node and uses RangedFilter to do the heavy lifting where possible
*
* @constructor
* @implements {NodeFilter}
*/
export class LimitedFilter implements NodeFilter {
/**
* @const
* @type {RangedFilter}
* @private
*/
private readonly rangedFilter_: RangedFilter;
/**
* @const
* @type {!Index}
* @private
*/
private readonly index_: Index;
/**
* @const
* @type {number}
* @private
*/
private readonly limit_: number;
/**
* @const
* @type {boolean}
* @private
*/
private readonly reverse_: boolean;
/**
* @param {!QueryParams} params
*/
constructor(params: QueryParams) {
this.rangedFilter_ = new RangedFilter(params);
this.index_ = params.getIndex();
this.limit_ = params.getLimit();
this.reverse_ = !params.isViewFromLeft();
}
/**
* @inheritDoc
*/
updateChild(
snap: Node,
key: string,
newChild: Node,
affectedPath: Path,
source: CompleteChildSource,
optChangeAccumulator: ChildChangeAccumulator | null
): Node {
if (!this.rangedFilter_.matches(new NamedNode(key, newChild))) {
newChild = ChildrenNode.EMPTY_NODE;
}
if (snap.getImmediateChild(key).equals(newChild)) {
// No change
return snap;
} else if (snap.numChildren() < this.limit_) {
return this.rangedFilter_
.getIndexedFilter()
.updateChild(
snap,
key,
newChild,
affectedPath,
source,
optChangeAccumulator
);
} else {
return this.fullLimitUpdateChild_(
snap,
key,
newChild,
source,
optChangeAccumulator
);
}
}
/**
* @inheritDoc
*/
updateFullNode(
oldSnap: Node,
newSnap: Node,
optChangeAccumulator: ChildChangeAccumulator | null
): Node {
let filtered;
if (newSnap.isLeafNode() || newSnap.isEmpty()) {
// Make sure we have a children node with the correct index, not a leaf node;
filtered = ChildrenNode.EMPTY_NODE.withIndex(this.index_);
} else {
if (
this.limit_ * 2 < newSnap.numChildren() &&
newSnap.isIndexed(this.index_)
) {
// Easier to build up a snapshot, since what we're given has more than twice the elements we want
filtered = ChildrenNode.EMPTY_NODE.withIndex(this.index_);
// anchor to the startPost, endPost, or last element as appropriate
let iterator;
if (this.reverse_) {
iterator = (newSnap as ChildrenNode).getReverseIteratorFrom(
this.rangedFilter_.getEndPost(),
this.index_
);
} else {
iterator = (newSnap as ChildrenNode).getIteratorFrom(
this.rangedFilter_.getStartPost(),
this.index_
);
}
let count = 0;
while (iterator.hasNext() && count < this.limit_) {
const next = iterator.getNext();
let inRange;
if (this.reverse_) {
inRange =
this.index_.compare(this.rangedFilter_.getStartPost(), next) <= 0;
} else {
inRange =
this.index_.compare(next, this.rangedFilter_.getEndPost()) <= 0;
}
Eif (inRange) {
filtered = filtered.updateImmediateChild(next.name, next.node);
count++;
} else {
// if we have reached the end post, we cannot keep adding elemments
break;
}
}
} else {
// The snap contains less than twice the limit. Faster to delete from the snap than build up a new one
filtered = newSnap.withIndex(this.index_);
// Don't support priorities on queries
filtered = filtered.updatePriority(
ChildrenNode.EMPTY_NODE
) as ChildrenNode;
let startPost;
let endPost;
let cmp;
let iterator;
if (this.reverse_) {
iterator = filtered.getReverseIterator(this.index_);
startPost = this.rangedFilter_.getEndPost();
endPost = this.rangedFilter_.getStartPost();
const indexCompare = this.index_.getCompare();
cmp = (a: NamedNode, b: NamedNode) => indexCompare(b, a);
} else {
iterator = filtered.getIterator(this.index_);
startPost = this.rangedFilter_.getStartPost();
endPost = this.rangedFilter_.getEndPost();
cmp = this.index_.getCompare();
}
let count = 0;
let foundStartPost = false;
while (iterator.hasNext()) {
let next = iterator.getNext();
if (!foundStartPost && cmp(startPost, next) <= 0) {
// start adding
foundStartPost = true;
}
let inRange =
foundStartPost && count < this.limit_ && cmp(next, endPost) <= 0;
if (inRange) {
count++;
} else {
filtered = filtered.updateImmediateChild(
next.name,
ChildrenNode.EMPTY_NODE
);
}
}
}
}
return this.rangedFilter_
.getIndexedFilter()
.updateFullNode(oldSnap, filtered, optChangeAccumulator);
}
/**
* @inheritDoc
*/
updatePriority(oldSnap: Node, newPriority: Node): Node {
// Don't support priorities on queries
return oldSnap;
}
/**
* @inheritDoc
*/
filtersNodes(): boolean {
return true;
}
/**
* @inheritDoc
*/
getIndexedFilter(): IndexedFilter {
return this.rangedFilter_.getIndexedFilter();
}
/**
* @inheritDoc
*/
getIndex(): Index {
return this.index_;
}
/**
* @param {!Node} snap
* @param {string} childKey
* @param {!Node} childSnap
* @param {!CompleteChildSource} source
* @param {?ChildChangeAccumulator} changeAccumulator
* @return {!Node}
* @private
*/
private fullLimitUpdateChild_(
snap: Node,
childKey: string,
childSnap: Node,
source: CompleteChildSource,
changeAccumulator: ChildChangeAccumulator | null
): Node {
// TODO: rename all cache stuff etc to general snap terminology
let cmp;
if (this.reverse_) {
const indexCmp = this.index_.getCompare();
cmp = (a: NamedNode, b: NamedNode) => indexCmp(b, a);
} else {
cmp = this.index_.getCompare();
}
const oldEventCache = snap as ChildrenNode;
assert(oldEventCache.numChildren() == this.limit_, '');
const newChildNamedNode = new NamedNode(childKey, childSnap);
const windowBoundary = this.reverse_
? oldEventCache.getFirstChild(this.index_)
: (oldEventCache.getLastChild(this.index_) as NamedNode);
const inRange = this.rangedFilter_.matches(newChildNamedNode);
if (oldEventCache.hasChild(childKey)) {
const oldChildSnap = oldEventCache.getImmediateChild(childKey);
let nextChild = source.getChildAfterChild(
this.index_,
windowBoundary,
this.reverse_
);
while (
nextChild != null &&
(nextChild.name == childKey || oldEventCache.hasChild(nextChild.name))
) {
// There is a weird edge case where a node is updated as part of a merge in the write tree, but hasn't
// been applied to the limited filter yet. Ignore this next child which will be updated later in
// the limited filter...
nextChild = source.getChildAfterChild(
this.index_,
nextChild,
this.reverse_
);
}
const compareNext =
nextChild == null ? 1 : cmp(nextChild, newChildNamedNode);
const remainsInWindow =
inRange && !childSnap.isEmpty() && compareNext >= 0;
if (remainsInWindow) {
if (changeAccumulator != null) {
changeAccumulator.trackChildChange(
Change.childChangedChange(childKey, childSnap, oldChildSnap)
);
}
return oldEventCache.updateImmediateChild(childKey, childSnap);
} else {
if (changeAccumulator != null) {
changeAccumulator.trackChildChange(
Change.childRemovedChange(childKey, oldChildSnap)
);
}
const newEventCache = oldEventCache.updateImmediateChild(
childKey,
ChildrenNode.EMPTY_NODE
);
const nextChildInRange =
nextChild != null && this.rangedFilter_.matches(nextChild);
if (nextChildInRange) {
Eif (changeAccumulator != null) {
changeAccumulator.trackChildChange(
Change.childAddedChange(nextChild.name, nextChild.node)
);
}
return newEventCache.updateImmediateChild(
nextChild.name,
nextChild.node
);
} else {
return newEventCache;
}
}
} else Iif (childSnap.isEmpty()) {
// we're deleting a node, but it was not in the window, so ignore it
return snap;
} else Eif (inRange) {
if (cmp(windowBoundary, newChildNamedNode) >= 0) {
if (changeAccumulator != null) {
changeAccumulator.trackChildChange(
Change.childRemovedChange(windowBoundary.name, windowBoundary.node)
);
changeAccumulator.trackChildChange(
Change.childAddedChange(childKey, childSnap)
);
}
return oldEventCache
.updateImmediateChild(childKey, childSnap)
.updateImmediateChild(windowBoundary.name, ChildrenNode.EMPTY_NODE);
} else {
return snap;
}
} else {
return snap;
}
}
}
|